using System;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace UserManagementService.Users
{
    /// <summary>
    /// 用户登录历史记录实体
    /// </summary>
    public class UserLoginHistory : FullAuditedEntity<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public virtual Guid UserId { get; protected set; }

        /// <summary>
        /// 登录时间
        /// </summary>
        public virtual DateTime LoginTime { get; protected set; }

        /// <summary>
        /// 登录IP地址
        /// </summary>
        public virtual string? IpAddress { get; protected set; }

        /// <summary>
        /// 浏览器信息
        /// </summary>
        public virtual string? BrowserInfo { get; protected set; }

        /// <summary>
        /// 操作系统信息
        /// </summary>
        public virtual string? OperatingSystem { get; protected set; }

        /// <summary>
        /// 设备信息
        /// </summary>
        public virtual string? DeviceInfo { get; protected set; }

        /// <summary>
        /// 登录地点
        /// </summary>
        public virtual string? Location { get; protected set; }

        /// <summary>
        /// 登录状态（0:成功 1:失败）
        /// </summary>
        public virtual LoginStatus Status { get; protected set; }

        /// <summary>
        /// 失败原因（当登录失败时）
        /// </summary>
        public virtual string? FailureReason { get; protected set; }

        /// <summary>
        /// 登录类型（0:密码登录 1:手机验证码 2:第三方登录 3:SSO）
        /// </summary>
        public virtual LoginType LoginType { get; protected set; }

        /// <summary>
        /// 会话ID
        /// </summary>
        public virtual string? SessionId { get; protected set; }

        /// <summary>
        /// 登出时间（可为空，表示尚未登出）
        /// </summary>
        public virtual DateTime? LogoutTime { get; protected set; }

        /// <summary>
        /// 会话持续时间（秒）
        /// </summary>
        public virtual long? SessionDuration { get; protected set; }

        protected UserLoginHistory()
        {
        }

        /// <summary>
        /// 创建登录历史记录
        /// </summary>
        public UserLoginHistory(
            Guid id,
            Guid userId,
            DateTime loginTime,
            string? ipAddress = null,
            LoginStatus status = LoginStatus.Success,
            LoginType loginType = LoginType.Password) : base(id)
        {
            UserId = userId;
            LoginTime = loginTime;
            IpAddress = Check.Length(ipAddress, nameof(ipAddress), maxLength: 64);
            Status = status;
            LoginType = loginType;
        }

        /// <summary>
        /// 设置登录成功
        /// </summary>
        public void SetSuccess()
        {
            Status = LoginStatus.Success;
            FailureReason = null;
        }

        /// <summary>
        /// 设置登录失败
        /// </summary>
        public void SetFailure(string failureReason)
        {
            Status = LoginStatus.Failed;
            FailureReason = Check.Length(failureReason, nameof(failureReason), maxLength: 256);
        }

        /// <summary>
        /// 设置浏览器信息
        /// </summary>
        public void SetBrowserInfo(string? browserInfo)
        {
            BrowserInfo = Check.Length(browserInfo, nameof(browserInfo), maxLength: 512);
        }

        /// <summary>
        /// 设置操作系统信息
        /// </summary>
        public void SetOperatingSystem(string? operatingSystem)
        {
            OperatingSystem = Check.Length(operatingSystem, nameof(operatingSystem), maxLength: 256);
        }

        /// <summary>
        /// 设置设备信息
        /// </summary>
        public void SetDeviceInfo(string? deviceInfo)
        {
            DeviceInfo = Check.Length(deviceInfo, nameof(deviceInfo), maxLength: 256);
        }

        /// <summary>
        /// 设置登录地点
        /// </summary>
        public void SetLocation(string? location)
        {
            Location = Check.Length(location, nameof(location), maxLength: 256);
        }

        /// <summary>
        /// 设置会话ID
        /// </summary>
        public void SetSessionId(string? sessionId)
        {
            SessionId = Check.Length(sessionId, nameof(sessionId), maxLength: 128);
        }

        /// <summary>
        /// 设置登出时间
        /// </summary>
        public void SetLogoutTime(DateTime logoutTime)
        {
            LogoutTime = logoutTime;
            if (LoginTime < logoutTime)
            {
                SessionDuration = (long)(logoutTime - LoginTime).TotalSeconds;
            }
        }
    }

    /// <summary>
    /// 登录状态枚举
    /// </summary>
    public enum LoginStatus
    {
        /// <summary>
        /// 成功
        /// </summary>
        Success = 0,

        /// <summary>
        /// 失败
        /// </summary>
        Failed = 1
    }

    /// <summary>
    /// 登录类型枚举
    /// </summary>
    public enum LoginType
    {
        /// <summary>
        /// 密码登录
        /// </summary>
        Password = 0,

        /// <summary>
        /// 手机验证码登录
        /// </summary>
        PhoneCode = 1,

        /// <summary>
        /// 第三方登录
        /// </summary>
        ThirdParty = 2,

        /// <summary>
        /// SSO单点登录
        /// </summary>
        SSO = 3,

        /// <summary>
        /// 扫码登录
        /// </summary>
        QRCode = 4
    }
}